package com.wtwd.device.service.impl;


import com.wtwd.device.util.JWTUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author zdl
 * @date 2021/11/5 13:21
 */
@Service
public class AppleUserServiceImpl extends ThirdPartyUserServiceImpl {

    private static final Logger log = LoggerFactory.getLogger(AppleUserServiceImpl.class);


    private static final String URL_GET_TOKEN = "https://oauth2.googleapis.com/token";
    private static final String CLIENT_ID = "Y72JFXY7Q5";
    private static final String CLIENT_SECRET = "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";
    private static final String REDIRECT_URI = "http://localhost:9001/app/app-user/google-login";
    private static final String GRANT_TYPE = "authorization_code";

    @Autowired
    private RestTemplate restSSLTemplate;

    @Override
    public ThirdPartyUserInfo getUserInfo(GetTokenResponseBase getTokenResponseBase) {
        String idToken = ((GetTokenResponseDTO) getTokenResponseBase).getId_token();
        ThirdPartyUserInfo thirdPartyUserInfo = new ThirdPartyUserInfo();
        String name = JWTUtil.getClaim(idToken, "name", String.class);
        thirdPartyUserInfo.setName(name);
        return thirdPartyUserInfo;
    }

    @Override
    public String getUserKey(GetTokenResponseBase getTokenResponseBase) {
        String idToken = ((GetTokenResponseDTO) getTokenResponseBase).getId_token();
        if (StringUtils.isBlank(idToken)) {
            return null;
        }
        return JWTUtil.getClaim(idToken, "sub", String.class);
    }

    @Override
    public GetTokenResponseBase getAccessToken(String code) {
        return restSSLTemplate.postForObject(URL_GET_TOKEN, new GetTokenRequstDTO(code), GetTokenResponseDTO.class);

    }

    class GetTokenRequstDTO {
        private String code;
        private String client_id;
        private String client_secret;
        private String redirect_uri;
        private String grant_type;

        public GetTokenRequstDTO(String code) {
            this.code = code;
            this.client_id = CLIENT_ID;
            this.client_secret = CLIENT_SECRET;
            this.redirect_uri = REDIRECT_URI;
            this.grant_type = GRANT_TYPE;
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getClient_id() {
            return client_id;
        }

        public void setClient_id(String client_id) {
            this.client_id = client_id;
        }

        public String getClient_secret() {
            return client_secret;
        }

        public void setClient_secret(String client_secret) {
            this.client_secret = client_secret;
        }

        public String getRedirect_uri() {
            return redirect_uri;
        }

        public void setRedirect_uri(String redirect_uri) {
            this.redirect_uri = redirect_uri;
        }

        public String getGrant_type() {
            return grant_type;
        }

        public void setGrant_type(String grant_type) {
            this.grant_type = grant_type;
        }
    }

    static class GetTokenResponseDTO extends GetTokenResponseBase {

        public GetTokenResponseDTO(String idToken) {
            super(idToken);
        }
    }
}
